#include "server.h"
#include <jni.h>
#include <memory>
#include <string>

std::unique_ptr<Server> gServer;
extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_grpcapp_MainActivity_startServer(JNIEnv *env, jobject thiz,
                                                  jstring port) {
  const char *port_str = env->GetStringUTFChars(port, nullptr);
  gServer = std::make_unique<Server>();
  return gServer->StartServer(port_str);
}

extern "C" JNIEXPORT void JNICALL
Java_com_example_grpcapp_MainActivity_stopServer(JNIEnv *env, jobject thiz) {
  if (gServer == nullptr)
    return;
  gServer->StopServer();
}

extern "C" JNIEXPORT jboolean JNICALL
Java_com_example_grpcapp_MainActivity_isServerRunning(JNIEnv *env,
                                                      jobject thiz) {
  if (gServer == nullptr)
    return false;
  return gServer->IsServerRunning();
}